package com.zmc.lostfound.post.controller;

import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.common.valid.AddGroup;
import com.zmc.lostfound.common.valid.UpdateGroup;
import com.zmc.lostfound.post.entity.PostStarEntity;
import com.zmc.lostfound.post.vo.PostStarSaveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zmc.lostfound.post.service.PostStarService;

import java.util.List;

/**
 * @author ZMC, gxc
 * @description 帖子&用户收藏管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:49:44
 */
@RestController
@RequestMapping("/postStar")
@Api(value = "帖子&用户收藏管理接口", tags = "帖子&用户收藏管理接口")
public class PostStarController {

    private final PostStarService postStarService;

    public PostStarController(PostStarService postStarService) {
        this.postStarService = postStarService;
    }

    @ApiOperation("按主键id查询帖子&收藏信息")
    @GetMapping("/query/infoById/{id}")
    public Result queryInfoById(@PathVariable("id") Long id) {
        PostStarEntity postStarInfo = postStarService.queryInfoById(id);
        return Result.ok("查询成功").put("postStarInfo", postStarInfo);
    }

    @ApiOperation("按帖子id查询帖子&收藏信息")
    @GetMapping("/query/infoByPostId/{postId}")
    public Result queryInfoByPostId(@PathVariable("postId") Long postId) {
        List<Long> userIds = postStarService.queryInfoByPostId(postId);
        return Result.ok("查询成功").put("userIds", userIds);
    }

    @ApiOperation("按用户id查询帖子&收藏信息")
    @GetMapping("/query/infoByUserId/{userId}")
    public Result queryInfoByUserId(@PathVariable("userId") Long userId) {
        List<Long> postIds = postStarService.queryInfoByUserId(userId);
        return Result.ok("查询成功").put("postIds", postIds);
    }

    @ApiOperation("判断用户是否收藏该帖子")
    @GetMapping("/{userId}/isLike/{postId}")
    public Result isLike(@PathVariable("userId") Long userId, @PathVariable("postId") Long postId) {
        Boolean isLike = postStarService.isLike(userId, postId);
        return Result.ok().put("isLike", isLike);
    }

    @ApiOperation("新增帖子&收藏信息")
    @PostMapping("/insertPostStar")
    public Result insertPostStar(@RequestBody @Validated(value = {AddGroup.class}) PostStarSaveVO postStarSaveVO) {
        return postStarService.insertPostUser(postStarSaveVO);
    }

    @ApiOperation("修改帖子&收藏信息")
    @PutMapping("/updatePostStar")
    public Result updatePostUser(@RequestBody @Validated(value = {UpdateGroup.class}) PostStarSaveVO postStarSaveVO) {
        return postStarService.updatePostUser(postStarSaveVO);
    }

    @ApiOperation("按主键ids批量删除帖子&收藏信息")
    @DeleteMapping("/deleteBatchByIds")
    public Result deleteBatchByIds(@RequestBody List<Long> ids) {
        return postStarService.deleteBatchByIds(ids);
    }

    @ApiOperation("按帖子id删除帖子&收藏信息")
    @DeleteMapping("/deleteBatchByPostId/{postId}")
    public Result deleteBatchByPostIds(@PathVariable("postId") Long postId) {
        return postStarService.deleteByPostIdAndUserId(postId, null);
    }

    @ApiOperation("按用户id删除帖子&收藏信息")
    @DeleteMapping("/deleteBatchByUserId/{userId}")
    public Result deleteBatchByUserIds(@PathVariable("userId") Long userId) {
        return postStarService.deleteByPostIdAndUserId(null, userId);
    }

    @ApiOperation("按帖子id和用户id删除帖子&收藏信息")
    @DeleteMapping("/delete/{postId}/and/{userId}")
    public Result deleteByPostIdAndUserId(@PathVariable("postId") Long postId, @PathVariable("userId") Long userId) {
        return postStarService.deleteByPostIdAndUserId(postId, userId);
    }

}
